﻿<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 我的消息
* */
class Notice extends Base_Controller
{
    private static $data = array();
    protected $models=['notice_model'];
    public function __construct()
    {
        parent::__construct();        
    }
    
    /**
     *@title 我的消息
     *@desc 我的消息(用于个人中心页)
     *
     *@input {"name":"userId","required","true","type":"int","desc":"当前登录用户ID"}
     *@input {"name":"page","type":"string","desc":"页数,不传则默认为0,即从第一页开始"}
     *@input {"name":"size","type":"string","desc":"每页显示条数,不传则默认为20"}
     *@input {"name":"type","type":"string","desc":"取值为read或unread,read为已读,unread为未读,不传此参数则默认获取所有消息数据"}
     *     
     *@output {"name":"code","type":"int","desc":"200:获取成功,400获取失败,无数据"}
     *@output {"name":"msg","type":"string","desc":"获取成功/获取失败"}
     *
     *@output {"name":"data","type":"array","desc":"课程数组,下面是详细说明,index代表该数组下标"}
     *@output {"name":"data[index].noticeId","desc":"消息ID","child":"1"}
     *@output {"name":"data[index].noticeContent","desc":"消息内容","child":"1"}
     *@output {"name":"data[index].readStatus","desc":"阅读状态,0未读,1已读","child":"1"}
     * */
    public function myNotice()
    {   
        $userId=$this->input->post('userId');       //登录用户ID
        if (!$userId){
            show3();
        }
        $type=$this->input->post('type');
        if($type=='unread'){
            $readStatus=0;
        }elseif($type=='read'){
            $readStatus=1;
        }
        $page=$this->input->post('page');
        $size=$this->input->post('size');
        $page=empty($page)?0:$page;
        $size=empty($size)?20:$size;
        $offset=$page*$size;
        $where=['userId'=>$userId,'noticeType'=>1];
        if (isset($readStatus)){
            $where['readStatus']=$readStatus;
        }
        $noticeList = $this->notice_model->getList($where, $size, $offset);
        if($noticeList){
            show200($noticeList);
        }else{
            show400();
        }
    }
    /**
     *@title 将消息标记为已读
     *@desc 将消息标记为已读
     *@input {"name":"noticeIds","require":"true","type":"array","desc":"消息ID,以数组形式传递(需要将数组转换为json字符串来传),可以标记一个或多个消息为已读,如传递[1],则为标记一个消息为已读,传递[1,2,3,],则标记三个消息为已读"}
     *
     *@output {"name":"code","type":"int","desc":"200:成功,400失败,3:未登录,300各种提示信息"}
     *@output {"name":"msg","type":"string","desc":"返回信息;标记成功/失败"}
     * */
    public function read()
    {
        $noticeIds=$this->input->post('noticeIds');
        $noticeIds=json_decode($noticeIds,true);
        if(!is_array($noticeIds)){
            show300('消息ID数组格式不正确');
        }
        if($noticeIds){
            show300('消息ID不能为空');
        }
        $noticeData = array('readStatus'=>1,'readTime'=>time());
        $bool = $this->notice_model->updateData($noticeIds,$noticeData);
        if($bool) show200([],'已标记为已读');
        else show400('标记已读失败');
    }
    
    
    
    
    
    
    
    
    
}